package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.AbsentClass;
import com.teacher.pojo.entity.VolunteerStar;
import com.teacher.pojo.enums.VolunteerStarType;
import com.teacher.pojo.excel.AbsentClassExcel;
import com.teacher.pojo.excel.VolunteerStarExcel;
import com.teacher.service.IAbsentClassService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.IVolunteerStarService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelVolunteerStarClassListener extends AnalysisEventListener<VolunteerStarExcel> {
    private Long termId;
    private IVolunteerStarService iVolunteerStarService;

    private List<VolunteerStar> volunteerStarList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelVolunteerStarClassListener(Long termId, IVolunteerStarService iVolunteerStarService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iVolunteerStarService = iVolunteerStarService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(VolunteerStarExcel volunteerStarExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(volunteerStarExcel.getStudentKey())) {
            VolunteerStar volunteerStar = new VolunteerStar();
            BeanUtils.copyProperties(volunteerStarExcel, volunteerStar);
            volunteerStar.setTermId(termId);
            if (volunteerStarExcel.getStar().equals(VolunteerStarType.ONE.getType())) {
                volunteerStar.setStar(VolunteerStarType.ONE.getCode());
            } else if (volunteerStarExcel.getStar().equals(VolunteerStarType.TWO.getType())) {
                volunteerStar.setStar(VolunteerStarType.TWO.getCode());
            } else if (volunteerStarExcel.getStar().equals(VolunteerStarType.THREE.getType())) {
                volunteerStar.setStar(VolunteerStarType.THREE.getCode());
            } else if (volunteerStarExcel.getStar().equals(VolunteerStarType.FOUR.getType())) {
                volunteerStar.setStar(VolunteerStarType.FOUR.getCode());
            } else {
                volunteerStar.setStar(VolunteerStarType.FIVE.getCode());
            }
            volunteerStarList.add(volunteerStar);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iVolunteerStarService.saveBatch(volunteerStarList);
    }
}
